package com.arlin.demo.controller;

import com.arlin.demo.entity.User;
import com.arlin.demo.service.UserService;
import com.arlin.spring.annotation.ioc.Controller;
import com.arlin.spring.annotation.ioc.Autowired;
import com.arlin.spring.annotation.mvc.*;
import com.arlin.spring.enums.RequestMethod;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: arlin
 * @Date: 2021/12/11
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get0")
    public User get(@RequestParam("id") Long id,
                    @RequestParam(value = "name", require = true, defaultValue = "default name") String name,
                    @RequestParam("age") Integer age) {
        return new User(id, name, age);
    }

    @GetMapping("get")
    public User getUser() {
        return userService.getUser();
    }

    @GetMapping("/{age}")
    public User get(@PathVariable("age") Integer age) {
        return new User(123456L, "测试@PathVariable", age);
    }

    @PostMapping("post")
    public User demo1() {
        return userService.getUser();
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public User demo2() {
        return userService.getUser();
    }

    @RequestMapping(value = {"getUser1", "/getUser2"}, method = {RequestMethod.GET, RequestMethod.POST})
    public User demo3() {
        return userService.getUser();
    }

    @PostMapping("/postTest")
    public UserDto create(@RequestBody User user) {
        return new UserDto(user.getName(), user.getAge());
    }

    static class UserDto {
        public String userName;
        public Integer age;

        public UserDto(String userName, Integer age) {
            this.userName = userName;
            this.age = age;
        }
    }
}
